package uu.game.majiang.core;

import java.util.ArrayList;

/**
 * 代表一圈，就是四家轮流坐一次庄，直到最后一家下庄。 
 * 
 * @author uu
 * @version 2011-2-27 下午09:33:36
 */
public final class Quan {
    
    /*
     * 圈风
     */
    private Feng           quanfeng;
    
    /*
     * 此圈已经开始了的盘。国标不连庄，一圈结束时肯定是4盘。 非国标一般可以连庄，不一定几盘
     */
    private ArrayList<Pan> pans = new ArrayList<Pan>(4);
    
    /*
     * 此圈是否已结束
     */
    private boolean        isFinished;
    
    /*
     * 所在的局
     */
    private Ju             ju;
    
    Quan(Ju ju, Feng quanfeng) {
        AbstractPlayer[] players = ju.getPlayers();
        players[0].setMenfeng(Feng.DONG);
        players[1].setMenfeng(Feng.NAN);
        players[2].setMenfeng(Feng.XI);
        players[3].setMenfeng(Feng.BEI);
        this.quanfeng = quanfeng;
        this.ju = ju;
    }
    
    /**
     * 当前所在的局
     * 
     * @return 当前局
     */
    public Ju getJu() {
        return ju;
    }
    
    /**
     * 圈风
     * 
     * @return 圈风
     */
    public Feng getQuanfeng() {
        return quanfeng;
    }
    
    /**
     * 得到所有开始了的盘。得到的最后一盘有可能并未结束。
     * 
     * @return 所有开始了的盘
     */
    public Pan[] getPans() {
        return pans.toArray(new Pan[pans.size()]);
    }
    
    /**
     * 当前盘。如果一盘也没开始，返回null。
     * 
     * @return 当前盘
     */   
    public Pan currentPan() {
        if (pans.size() == 0)
            return null;
        return pans.get(pans.size() - 1);
    }
    
    /**
     * 新开一盘。返回null表示此圈已结束。
     * 
     * @return 新开始的一盘
     */
    public Pan nextPan() {
        if (isFinished)
            return null; // 此圈已结束
            
        AbstractPlayer[] players = ju.getPlayers();
        AbstractPlayer player1 = players[0];
        AbstractPlayer player2 = players[1];
        AbstractPlayer player3 = players[2];
        AbstractPlayer player4 = players[3];
        
        if (pans.size() != 0) {
            Pan lastPan = pans.get(pans.size() - 1);
            AbstractPlayer hp = lastPan.getHuPlayer();
            if (lastPan.isHuangzhuang() || hp.getMenfeng() != Feng.DONG) {
                if (player1.getMenfeng() == Feng.DONG) {
                    player2.setMenfeng(Feng.DONG);
                    player3.setMenfeng(Feng.NAN);
                    player4.setMenfeng(Feng.XI);
                    player1.setMenfeng(Feng.BEI);
                } else if (player2.getMenfeng() == Feng.DONG) {
                    player3.setMenfeng(Feng.DONG);
                    player4.setMenfeng(Feng.NAN);
                    player1.setMenfeng(Feng.XI);
                    player2.setMenfeng(Feng.BEI);
                } else if (player3.getMenfeng() == Feng.DONG) {
                    player4.setMenfeng(Feng.DONG);
                    player1.setMenfeng(Feng.NAN);
                    player2.setMenfeng(Feng.XI);
                    player3.setMenfeng(Feng.BEI);
                } else if (player4.getMenfeng() == Feng.DONG) {
                    player1.setMenfeng(Feng.DONG);
                    player2.setMenfeng(Feng.NAN);
                    player3.setMenfeng(Feng.XI);
                    player4.setMenfeng(Feng.BEI);
                    player1.reset();
                    player2.reset();
                    player3.reset();
                    player4.reset();
                    // 这圈已结束
                    isFinished = true;
                    // 如果是所在一局的最后一圈，则此局也结束
                    if (ju.getQuanNum() == 4)
                        ju.setFinished(true);
                    return null;
                }
            }
        }
        Pan pan = new Pan(this);
        pans.add(pan);
        return pan;
    }
    
    /**
     * 判断本圈是否已结束。最后一个玩家下庄则本圈结束
     * 
     * @return 本圈是否已结束
     */
    public boolean isFinished() {
        return isFinished;
    }
}
